package fr.labri.macchiato.client.project;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.FocusEvent;
import com.smartgwt.client.widgets.form.fields.events.FocusHandler;
import com.smartgwt.client.widgets.layout.HLayout;

/**
 * Base widget used to display all type of valid or cancel prompt
 * 
 * @author Alassane SESSOUMA
 *
 */
public abstract class AbstractPrompt extends Window{
	
	private static final String VALIDATE = "Valider";
	private static final String CANCEL = "Annuler";

	/**
	 * The valid button
	 */
	protected IButton validateButton;
	/**
	 * The cancel button
	 */
	protected IButton cancelButton;
	/**
	 * The layout used to display the valid and cancel button
	 */
	protected HLayout buttonLayout;
	/**
	 * Item used to display editable text
	 */
	protected TextItem textItem;
	/**
	 * Item used to set the budget
	 */
	protected TextItem budgetItem;
	

	/**
	 * Getter for the cancel button
	 * 
	 * @return The cancel button
	 */
	public IButton getCancelButton() {
		return cancelButton;
	}

	/**
	 * Getter for the bugdet textitem
	 * 
	 * @return The budget text field
	 */
	public TextItem getBudgetItem() {
		return budgetItem;
	}

	/**
	 * The form for the prompt
	 */
	protected DynamicForm form;

	/**
	 * Getter for the form
	 * 
	 * @return The form
	 */
	public DynamicForm getForm() {
		return form;
	}

	/**
	 * Getter for the valid button
	 * 
	 * @return The validation button
	 */
	public IButton getValidateButton() {
		return validateButton;
	}

	/**
	 * Getter for the standard textitem
	 * 
	 * @return The standard text field
	 */
	public TextItem getTextItem() {
		return textItem;
	}

	/**
	 * Create a base valid or cancel prompt
	 * 
	 * @param promptTitle The title of the prompt
	 * @param textItemTitle The title of the standard text field
	 */
	public AbstractPrompt(String promptTitle, String textItemTitle) {
		
		setWindowProperties(promptTitle);

		validateButton = new IButton(VALIDATE);
		validateButton.setDisabled(true);
		cancelButton = new IButton(CANCEL);

		buttonLayout = new HLayout(15);
		buttonLayout.setMargin(20);
		buttonLayout.addMember(validateButton);
		buttonLayout.addMember(cancelButton);
						
		textItem = new TextItem();
		textItem.setTitle(textItemTitle);
		textItem.setWrapTitle(false);
		textItem.setSelectOnFocus(true);
		textItem.setCanFocus(true);
		
		budgetItem = new TextItem();
		budgetItem.setTitle("Budget souhaité");
		budgetItem.setWrapTitle(false);
		budgetItem.setSelectOnFocus(true);
		budgetItem.setCanFocus(true);
		

		budgetItem = new TextItem();
		budgetItem.setTitle("Budget souhaité");
		budgetItem.setWrapTitle(false);
		budgetItem.setSelectOnFocus(true);
		budgetItem.setCanFocus(true);
		
		addHandlers();
		
		form = new DynamicForm();
		form.setAutoFocus(true);
		form.setMargin(20);
		form.setFields(textItem,budgetItem);
		addItem(form);
		addItem(buttonLayout);
	}

	/**
	 * Getter for the valid and cancel button layout
	 * 
	 * @return The buttons layout
	 */
	public HLayout getButtons() {
		return buttonLayout;
	}

	private void addHandlers() {

		cancelButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				destroy();
			}
		});
		textItem.addFocusHandler(new FocusHandler() {

			@Override
			public void onFocus(FocusEvent event) {
				validateButton.setDisabled(false);
			}
		});
		
	
		budgetItem.addFocusHandler(new FocusHandler() {

			@Override
			public void onFocus(FocusEvent event) {
				validateButton.setDisabled(false);
			}
		});
	}

	private void setWindowProperties(String title) {
		setIsModal(true);
		setShowModalMask(true);
		setAutoCenter(true);
		setAutoSize(true);
		setShowCloseButton(true);
		setShowMinimizeButton(false);
		setShowTitle(true);
		setAlign(Alignment.CENTER);
		setTitle(title);
	}
}

